package com.hsmw.schedule.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * @author WD
 * @version 1.0
 * @description: TODO
 * @date 2021/3/19 9:40
 */
@Configuration
@EnableAsync
@Slf4j
public class ExecutorConfig {


	@Bean("order")
	public Executor orderScheduledTaskExecutor() {
		ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
		// 核心线程数量：当前机器的核心数
		executor.setCorePoolSize(Runtime.getRuntime().availableProcessors());
		// 最大线程数
		executor.setMaxPoolSize(Runtime.getRuntime().availableProcessors() * 2);
		// 队列大小
		executor.setQueueCapacity(Integer.MAX_VALUE);
		// 线程池中的线程名前缀
		executor.setThreadNamePrefix("schedule-");
		//拒绝策略
		executor.setRejectedExecutionHandler(new ThreadPoolExecutor.AbortPolicy());
		return executor;
	}

}
